//
//  NewsTableViewCell.m
//  
//
//  Created by admin on 17/3/2.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "NewsTableViewCell.h"
#import <UIImageView+WebCache.h>

@interface NewsTableViewCell ()

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;//标题
@property (weak, nonatomic) IBOutlet UIImageView *imgsrcImageView;//右侧图像
@property (weak, nonatomic) IBOutlet UILabel *sourceLabel;//新闻来源
@property (weak, nonatomic) IBOutlet UILabel *uptimeLabel;//时间
@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *imgsrcImageViews;//多张配图

@end

@implementation NewsTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];

    
}



- (void)setNews:(NewsModel *)news {
 
    _news = news;
    
    self.titleLabel.text = news.title;
    NSArray *selectedData = [[NSUserDefaults standardUserDefaults] objectForKey:@"selectedData"];
    BOOL isSelected = [selectedData containsObject:news.title];
    if (isSelected) {
        self.titleLabel.textColor = [UIColor colorWithRed:0.71 green:0.71 blue:0.72 alpha:1.00];
    }else{
        self.titleLabel.textColor = [UIColor blackColor];
    }
//浏览次数
//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//    if ([userDefaults integerForKey:news.title] == 0) {
//        news.pv = (arc4random_uniform(255)+1+2000) * (arc4random_uniform(8)+1);
//        [userDefaults setInteger:news.pv forKey:news.title];
//    }
//    news.pv = [userDefaults integerForKey:news.title];
    
    //来源
    self.sourceLabel.text = [NSString stringWithFormat:@"来源: %@",news.source];
    
    //时间戳转化成时间
    NSDateFormatter *stampFormatter = [[NSDateFormatter alloc] init];
    [stampFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    self.uptimeLabel.text = [LSZSpecialPackaging GetThePreviousTimeAndCompareItNow:news.uptime];

//    if (news.cover == nil || news.cover.count == 0)
//    {
////        if ([news.cover[0] isEqualToString:@""]) {
////            self.imgsrcImageView.image = [UIImage imageNamed:@""];
////        }
//        return;
//    }
//    [self.imgsrcImageView sd_setImageWithURL:[NSURL URLWithString:news.cover[0]] placeholderImage:[UIImage imageNamed:@"小的默认图片"]];
    if (news.cover.count == 1 || news.cover.count == 2) {
        [self.imgsrcImageView sd_setImageWithURL:[NSURL URLWithString:news.cover[0]] placeholderImage:[UIImage imageNamed:@"小的默认图片"]];
    }
    if (news.cover.count > 2) {
////        // 遍历
//        for (NSInteger i = 0; i < self.imgsrcImageViews.count; i++) {
//
//            // 循环取出里面的UIImageView
//            UIImageView *imgView = self.imgsrcImageViews[i];
//
//            // 循环取出图片地址
//            NSString *imgsrc = news.image30;
//
//            if ( i == 0 ) {
//                imgsrc = news.image30;
//                JKLog(@"image30 = %@", imgsrc);
//            }
//            if ( i == 1 ) {
//                imgsrc = news.image31;
//                JKLog(@"image31 = %@", imgsrc);
//            }
//            if ( i == 2 ) {
//                imgsrc = news.image32;
//                JKLog(@"image32 = %@", imgsrc);
//            }
//            // 下载图片
//            [imgView sd_setImageWithURL:[NSURL URLWithString:imgsrc] placeholderImage:[UIImage imageNamed:@"小的默认图片"]];
//        }
        
            // 遍历
            for (NSInteger i = 0; i < self.imgsrcImageViews.count; i++) {
                // 循环取出里面的UIImageView
                UIImageView *imgView = self.imgsrcImageViews[i];
                // 下载图片
                [imgView sd_setImageWithURL:[NSURL URLWithString:news.cover[i]] placeholderImage:[UIImage imageNamed:@"小的默认图片"]];
            }
    }
//    JKLog(@"news.cover = %@", news.cover);
}

@end
